package c.a.md.androidvideo.gles.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

/**
 * Created by ytf on 2018/10/31.
 * Description:
 */
public class BufferTransformUtil
{
    public static IntBuffer intBufferUtil(int[] arr)
    {
        IntBuffer mBuffer;
        // 初始化ByteBuffer，長度為arr數組的長度*4，因為一個int佔4個字節
        ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
        // 數組排列用nativeOrder
        qbb.order(ByteOrder.nativeOrder());
        mBuffer = qbb.asIntBuffer();
        mBuffer.put(arr);
        mBuffer.position(0);
        return mBuffer;
    }

    public static FloatBuffer floatBufferUtil(float[] arr)
    {
        FloatBuffer mBuffer;
        // 初始化ByteBuffer，長度為arr數組的長度*4，因為一個int佔4個字節
        ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
        // 數組排列用nativeOrder
        qbb.order(ByteOrder.nativeOrder());
        mBuffer = qbb.asFloatBuffer();
        mBuffer.put(arr);
        mBuffer.position(0);
        return mBuffer;
    }

    public static ShortBuffer shortBufferUtil(short[] arr)
    {
        ShortBuffer mBuffer;
        // 初始化ByteBuffer，長度為arr數組的長度*2，因為一個short佔2個字節
        ByteBuffer dlb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 2 bytes per short)
                arr.length * 2);
        dlb.order(ByteOrder.nativeOrder());
        mBuffer = dlb.asShortBuffer();
        mBuffer.put(arr);
        mBuffer.position(0);
        return mBuffer;
    }
}
